package com.reebake.ideal.storage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.storage.entity.FileDetail;
import com.reebake.ideal.storage.entity.FileInfoDTO;
import com.reebake.ideal.storage.entity.FileListViewDTO;
import com.reebake.ideal.storage.service.FileDetailService;
import com.reebake.ideal.storage.service.FileOperationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/storage/file")
public class FileOperationController {
    private final FileOperationService fileOperationService;
    private final FileDetailService fileDetailService;


    @GetMapping("file-info-by-url")
    public FileInfoDTO fileInfo(String url) {
        return fileOperationService.fileInfo(url);
    }

    @GetMapping("file-info-by-filename")
    public FileInfoDTO fileInfo(@RequestParam(required = false) String platform, String path, String filename) {
        return fileOperationService.fileInfo(platform, path, filename);
    }

    @PostMapping("exists-by-url")
    public Boolean exists(String url) {
        return fileOperationService.exists(url);
    }

    @PostMapping("exists-by-filename")
    public Boolean exists(@RequestParam(required = false) String platform, String path, String filename) {
        return fileOperationService.exists(platform, path, filename);
    }

    @PostMapping("delete-by-url")
    public void delete(String url) {
        fileOperationService.delete(url);
    }

    @PostMapping("delete-by-ids")
    public void delete(@RequestBody String[] ids) {
        LambdaQueryWrapper<FileDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FileDetail::getFileDetailId, ids);
        List<FileDetail> fileDetails = fileDetailService.list(queryWrapper);
        for(FileDetail fileDetail:fileDetails) {
            fileOperationService.delete(fileDetail.getUrl());
        }
    }

    @PostMapping("delete-by-filename")
    public void delete(@RequestParam(required = false) String platform, String path, String filename) {
        fileOperationService.delete(platform, path, filename);
    }

    @PostMapping("copy")
    public FileInfoDTO copy(String url, @RequestParam(required = false) String platform, String path) {
        return fileOperationService.copy(url, platform, path);
    }

    @PostMapping("move")
    public FileInfoDTO move(String url, @RequestParam(required = false) String platform, String path) {
        return fileOperationService.move(url, platform, path);
    }

    @GetMapping("list")
    public FileListViewDTO list(@RequestParam(required = false) String platform, String path, @RequestParam(required = false) String marker, @RequestParam(required = false) Integer maxFiles) {
        return fileOperationService.list(platform, path, marker, maxFiles);
    }

}
